package com.wuhao.code.check.completion

import com.aegis.mybatis.xmlless.resolver.ExpressionResolver
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.psi.PsiReferenceContributor
import com.wuhao.code.check.highlight.XmllessMethodAnnotationHighlighter

/**
 *
 * @author 吴昊
 * @date 2024/2/1 6:41
 * @since v0.0.0
 * @version 1.0
 */

abstract class BaseXmllessReferenceContributor: PsiReferenceContributor() {


  protected fun create(
      value: String,
      element: PsiElement
  ): Array<PsiReference> {
    val nodes = ExpressionResolver.parseExpression(value.drop(1).dropLast(1)).getNodes()
    val res = mutableListOf<PsiReference>()
    nodes.forEach {
      if (it::class.java.simpleName == "Token" && it.toSql() !in XmllessMethodAnnotationHighlighter.Util.SQL_KEYWORDS) {
        val textRange = TextRange(it.start + 1, it.end + 2)
        res.add(XmllessReference(element, textRange))
      }
    }
    return res.toTypedArray()
  }

}
